package com.shz.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shz.config.MyPasswordEncoder;
import com.shz.pojo.User;
import com.shz.mapper.UserMapper;
import com.shz.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shz.utils.UserUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.shz.utils.RedisConstants.CACHE_USER_KEY;
import static com.shz.utils.RedisConstants.CACHE_USER_TTL;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yixin
 * @since 2024-06-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService, UserDetailsService {
    @Resource
    MyPasswordEncoder passwordEncoder;



    @Resource
    StringRedisTemplate stringRedisTemplate;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = query().eq("username", username).one();
        if (user == null) {
            //避免返回null，这里返回一个不含有任何值的User对象，在后期的密码比对过程中一样会验证失败
            return null;
        }
        //查询用户的角色信息，并返回存入user中
        return user;
    }


    @Override
    public boolean compareUserName(String username) {
        return !UserUtil.getCurrentUser().getUsername().equals(username);
    }




    @Override
    public boolean updateUserMsg(User user, String curpassword) {
        int uid=UserUtil.getCurrentUser().getId();
        String key = CACHE_USER_KEY + uid;
        stringRedisTemplate.delete(key);
        if(curpassword!=null&&curpassword.length()!=0){
            user.setPassword(passwordEncoder.encode(curpassword));
        }
        user.setId(uid);
        int roleName = UserUtil.getCurrentUser().getRoleName();
        user.setRoleName(roleName);
        return updateById(user);
    }



    @Override
    public boolean compareTwoPassword(String password) {
        String encode = passwordEncoder.encode(password);
        return !encode.equals(UserUtil.getCurrentUser().getPassword());
    }


    public  User getAUserOrUserCache(Integer uid){
        String key = CACHE_USER_KEY + uid;
        String s = stringRedisTemplate.opsForValue().get(key);
        User user = null;
        if (StrUtil.isNotBlank(s)) {
            user = JSONUtil.toBean(s, User.class);
        } else {
            user = query().eq("id", uid).one();
            if (user != null) {
                stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(user)
                        , CACHE_USER_TTL, TimeUnit.HOURS);
            }
        }
        if (user == null) {
            return null;
        }
        return user;
    }

}
